package org.ah.ssm.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.ah.ssm.ahutils.AhConst;
import org.ah.ssm.ahutils.AhUtils;
import org.ah.ssm.entity.Examinee;
import org.ah.ssm.entity.Question;
import org.ah.ssm.service.QuestionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

@SessionAttributes("qlist")
@Controller
@RequestMapping("ctr_question")
public class QuestionController {
	@Resource
	private QuestionService _service;

	/**
	 * 取题，发给用户端
	 * 
	 * @return
	 */
	@RequestMapping(value = "ctr_method_qs", method = RequestMethod.GET)
	public ModelAndView c_getQuestions(Map<String, Object> map) {
		List<Question> qs = _service.getQs();
		map.put(AhConst.ATTR_QS, qs);
		return new ModelAndView(AhConst.JSP_SHOW_QS);
	}

	@RequestMapping(value = "ctr_method_submit", method = RequestMethod.POST)
	public ModelAndView c_userSubmit(HttpServletRequest request) {
		HttpSession session = request.getSession();

		// 1.取用户信息
		Examinee eUser = (Examinee) session.getAttribute(AhConst.ATTR_EXAMINEE);
		// 2.取题目信息
		@SuppressWarnings("unchecked")
		List<Question> qs = (List<Question>) session
				.getAttribute(AhConst.ATTR_QS);
		// 3.取答题信息
		Map<String, String[]> parameterMap = request.getParameterMap();
		// 4.调用Service去处理
		String score = _service.getUserAnswers(eUser, qs, parameterMap);

		// 记录：用户已答过题
		AhUtils.whoAnswered.put(eUser.getExaName(), score);
		ModelAndView mv = new ModelAndView();
		mv.addObject(AhConst.ATTR_SCORE, score);
		mv.setViewName(AhConst.JSP_SCORE);

		return mv;
	}
}
